<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-3-19
 * Time: 下午12:00
 */

namespace api\modules\jzg\controllers;


use common\components\jingzhengu\Config;
use common\components\jingzhengu\Jzg;
use common\components\tools\Api;
use common\components\tools\Model;
use common\components\tools\Tools;
use common\models\credit_app\Estimate;
use common\models\JzgBack5;
use common\models\JzgPlace9Order;
use wodrow\yii\rest\ApiException;
use wodrow\yii\rest\Controller;

class NotifyController extends Controller
{
    /**
     * 精真估下单回调
     * @desc get/post
     * @param string $Status 订单状态(1.成功(评估成功) 2.退回)
     * @param string $OrderNum 订单编号
     * @param string $UserId 订单人创建人Id
     * @param string $Sign 加密参数
     * @param string $StatusDes 订单状态描述
     * @param string $TokenId 第三方tokenid
     * @param string $Message 消息内容（如:退回原因）
     */
    public function actionBack5($Status, $OrderNum, $UserId, $Sign, $StatusDes, $TokenId, $Message = '')
    {
        $this->onlyDataOut = true;
        $b1 = $UserId == Config::$userId;
        $b2 = $TokenId == Config::$tokenId;
        if (!$b1 || !$b2){
            return Jzg::notify(103, "参数检查错误");
        }
        $place9_order = JzgPlace9Order::findOne(['order_num' => $OrderNum]);
        if (!$place9_order){
            return Jzg::notify(113, "没有找到单子");
        }
        $place9_order->jzg_order_num = $OrderNum;
        $estimate = Estimate::find()->where(['et_other_onum' => $place9_order->order_num])->orderBy(['et_id' => SORT_DESC])->one();
        if (!$estimate){
            return Jzg::notify(123, "没有找到单子");
        }
        if ($Status == 1){
            $resp1 = Api::request('/jzg/default/order-info', ['orderNum' => $OrderNum, 'detailType' => 1]);
            $resp_arr1 = json_decode($resp1->content, true);
            if ($resp_arr1['code'] != 200){
                return Jzg::notify(107, $resp_arr1['message']);
            }
            if ($resp_arr1['data']['plat_resp']['Status'] != 100){
                return Jzg::notify(108, $resp_arr1['data']['plat_resp']['Msg']);
            }
            $plat_data1 = $resp_arr1['data']['plat_resp']['Data'];
            if ($place9_order->vin != $plat_data1['vin']){
                Tools::log($_REQUEST, 'jzg_notify_debug');
                return Jzg::notify(109, "回调的vin不匹配");
            }
            if ($estimate->et_status == '委托中'){
                if ($plat_data1['assessmentPriceB2C'] <= 200){
                    $estimate->et_price = 200;
                }else{
                    $pattern = "/希望车价([0-9|.]+)元/";
                    preg_match($pattern, $estimate->et_remarks, $matches);
                    $x = $matches[1];# et_remarks 期望车架
                    if ($x <= $plat_data1['assessmentPriceB2C']){
                        $estimate->et_price = $x;
                    }else{
                        $y = $plat_data1['assessmentPriceB2C'] * 1.15;
                        $z = (int)($y/1000);
                        $y = $z *1000;
                        if ($x <= $y){
                            if ($x <= $plat_data1['manufacturerPrice']){
                                $estimate->et_price = $x;
                            }else{
                                $estimate->et_price = $plat_data1['manufacturerPrice'];
                            }
                        }else{
                            if ($y <= $plat_data1['manufacturerPrice']){
                                $estimate->et_price = $y;
                            }else{
                                $estimate->et_price = $plat_data1['manufacturerPrice'];
                            }
                        }
                    }
                }
                $estimate->et_price_one = $estimate->et_price;
                $estimate->et_status = "已评估";
//                $estimate->et_status = "待评估";
                $plat_data1['assessmentDate'] = date("Y-m-d H:i:s");
                $place9_order->assessment_price_b2c = $plat_data1['assessmentPriceB2C'];
                $place9_order->assessment_price_c2b = $plat_data1['assessmentPriceC2B'];
                $place9_order->max_assessment_price_b2c = $plat_data1['maxAssessmentPriceB2C'];
                $place9_order->resp1_data = json_encode($plat_data1, JSON_UNESCAPED_UNICODE);
                if (!$place9_order->save()){
                    return Jzg::notify(112, Model::getModelError($place9_order));
                }
                $estimate->et_firm_return = $place9_order->resp1_data;
                if (!$estimate->save()){
                    return Jzg::notify(113, Model::getModelError($estimate));
                }
            }
            $jzg_back5 = new JzgBack5();
            $jzg_back5->status = $Status;
            $jzg_back5->order_num = $OrderNum;
            $jzg_back5->user_id = $UserId;
            $jzg_back5->token_id = $TokenId;
            $jzg_back5->sign = $Sign;
            $jzg_back5->status_des = $StatusDes;
            $jzg_back5->message = $Message;
            $jzg_back5->vin = $plat_data1['vin'];
            $jzg_back5->assessment_price_b2c = $plat_data1['assessmentPriceB2C'];
            $jzg_back5->assessment_price_c2b = $plat_data1['assessmentPriceC2B'];
            $jzg_back5->max_assessment_price_b2c = $plat_data1['maxAssessmentPriceB2C'];
            $jzg_back5->resp1_data = json_encode($plat_data1, JSON_UNESCAPED_UNICODE);
            $jzg_back5->created_at = time();
            if (!$jzg_back5->save()){
                return Jzg::notify(111, Model::getModelError($jzg_back5));
            }
            return Jzg::notify();
        }
        if ($Status == 2){
            $estimate->et_status = "退回";
            $estimate->et_fremarks = $Message;
            $jzg_back5 = new JzgBack5();
            $jzg_back5->status = $Status;
            $jzg_back5->order_num = $OrderNum;
            $jzg_back5->user_id = $UserId;
            $jzg_back5->token_id = $TokenId;
            $jzg_back5->sign = $Sign;
            $jzg_back5->status_des = $StatusDes;
            $jzg_back5->message = $Message;
            $jzg_back5->created_at = time();
            if (!$jzg_back5->save()){
                return Jzg::notify(104, Model::getModelError($jzg_back5));
            }
            if (!$place9_order->save()){
                return Jzg::notify(105, Model::getModelError($place9_order));
            }
            if (!$estimate->save()){
                return Jzg::notify(106, Model::getModelError($estimate));
            }
            return Jzg::notify();
        }
    }
}